/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.interceptors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>Project: smvc2022 - TestInterceptor
 * <p>Powered by webrx On 2022-04-07 09:37:58
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */


public class TestInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("1.preHandle（）......");

        //如果是SpringMVC请求
        if(handler instanceof HandlerMethod){
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            System.out.println("当前拦截的方法为："+handlerMethod.getMethod().getName());
            System.out.println(("当前拦截的方法参数长度为："+handlerMethod.getMethod().getParameters().length));
            System.out.println("当前拦截的方法为："+handlerMethod.getBean().getClass().getName());
            System.out.println("开始拦截---------");
            String uri = request.getRequestURI();
            System.out.println("拦截的uri："+uri);
        }

        boolean f = true;
        if(request.getSession().getAttribute("user") == null){
            f = false;
            request.setAttribute("msg", "请登录，没有登录不允许操作。");
            request.getRequestDispatcher("/admin/login").forward(request,response);
        }else{
            f = true;
        }



        //return HandlerInterceptor.super.preHandle(request, response, handler);
        return f;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("2.postHandle（）......");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("3.afterCompletion（）......");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
